#include "mainwindow.h"
#include "rsa.h"
#include "cipherfactory.h"
#include "fileoperation.h"
#include <QDebug>
#include <QApplication>


void createTestData(QByteArray& data, int size)
{
    data.resize(size);
    for (int i = 0; i < size; i++)
    {
        data[i] = i % 15;
    }
}

void testEvp(const QByteArray& data)
{
    QByteArray plainText = data;
    QByteArray encryptText;
    QByteArray decryptText;

    QByteArray key = QByteArray::fromHex("8cc72b05705d5c46f412af8cbed55aad");
    QByteArray ivec = QByteArray::fromHex("667b02a85c61c786def4521b060265e8");

    QByteArray key2 = QByteArray::fromHex("8cc72b05705d5c46f412af8cbed55aad8cc72b05705d5c46");//24位的key
    QByteArray ivec2 = QByteArray::fromHex("667b02a85c61c786");  //8位的ivec

    // EvpAES ecb模式加密验证
    CCipherFactory fac0;
    CCipher *cipher0 = fac0.buildCipher(0);
    cipher0->encrypt(plainText,encryptText,key,true,0);
    cipher0->encrypt(encryptText,decryptText,key,false,0);
    qDebug() << "Cipherfactory AES ecb encrypt verify" << ((decryptText == plainText) ? "succeeded" : "failed");
    encryptText.clear();
    decryptText.clear();

    // EvpDES cbc模式加密验证
    CCipherFactory fac1;
    CCipher *cipher1 = fac1.buildCipher(1);
    cipher1->encrypt(plainText,encryptText,key,true,1,ivec);
    cipher1->encrypt(encryptText,decryptText,key,false,1,ivec);
    qDebug() << "EvpDES cbc encrypt verify" << ((decryptText == plainText) ? "succeeded" : "failed");
    encryptText.clear();
    decryptText.clear();

    //Evp3DES cbc模式加密验证
    CCipherFactory fac2;
    CCipher *cipher2 = fac2.buildCipher(2);
    cipher2->encrypt(plainText,encryptText,key2,true,1,ivec2);
    cipher2->encrypt(encryptText,decryptText,key2,false,1,ivec2);
    qDebug() << "Evp3DES ecb encrypt verify" << ((decryptText == plainText) ? "succeeded" : "failed");
    encryptText.clear();
    decryptText.clear();

    //验证hash
    CCipherFactory fac3;
    CCipher *cipher3 = fac3.buildCipher(3);
    cipher3->encrypt(plainText,encryptText,0);
    qDebug() << "Hash SHA1 encrypt verify size():" << encryptText.size();
    encryptText.clear();

    // //验证rsa:密钥长度为2048，2048/8=256，但因转换为16进制后长度加倍，故只能加密256-11/2=122个字节的明文。
    // size_t len1;
    // size_t len2;
    // rsa kk;
    // kk.encrypt(plainText,encryptText,len1);
    // kk.decrypt(encryptText,len1,decryptText,len2);
    // qDebug() << "Rsa encrypt verify" << ((decryptText == plainText) ? "succeeded" : "failed") <<"!!!\n";
    // encryptText.clear();
    // decryptText.clear();

}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QByteArray data;
    createTestData(data,1*1024*1024+3);
    testEvp(data);

    MainWindow w;
    w.show();

    return a.exec();
}
